package com.test.moudle.sys.controller;

import com.test.R;
import com.test.moudle.sys.entity.User;
import com.test.moudle.sys.form.LoginForm;
import com.test.moudle.sys.form.UserFrom;
import com.test.moudle.sys.service.TokenService;
import com.test.moudle.sys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Api(tags = "系统用户管理")
@RequestMapping("user")
@RestController
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private TokenService tokenService;

    @ApiOperation("用户登录")
    @PostMapping("login")
    public R login(@RequestBody LoginForm loginForm) {

        log.info("用户登录|{}", loginForm);

        User user = userService.findByName(loginForm.getUsername());
        log.info("user: {}", user);

        if (user == null || !user.getPasswd().equals(loginForm.getPassword())) {
            return R.error("账号或密码不正确");
        }

        if (user.getStatus() == 1) {
            return R.error("账号已被锁定,请联系管理员");
        }

        return tokenService.create(user.getId());
    }

    @ApiOperation("用户退出")
    @GetMapping("logout")
    public Map<String, Object> logout() {

        User user = (User) SecurityUtils.getSubject().getPrincipal();
        tokenService.delete(user.getId());
        log.info("用户退出|{}", user);

        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        return R.ok("退出成功").put("status", 200);
    }

    @ApiOperation("用户信息")
    @GetMapping("info")
    public R info() {

        User user = (User) SecurityUtils.getSubject().getPrincipal();
        log.info("用户信息|{}", user);

        Map<String, Object> data = new HashMap<>();
        data.put("name", user.getName());
        data.put("avatar", user.getAvatar());
        // todo 所有用户暂且都是admin角色
        data.put("roles", new String[]{"admin"});

        return R.ok("查询成功").put("data", data);
    }

    @ApiOperation("创建用户")
    @PostMapping("register")
    public R register(@RequestBody UserFrom userAddFrom) {

        if (userAddFrom.getUsername() == null || userAddFrom.getUsername().trim().equals("")) return R.error("用户名格式不正确");

        if (userAddFrom.getPassword() == null || userAddFrom.getPassword().trim().equals("")) return R.error("密码格式不正确");

        if (userAddFrom.getAvatar() == null || userAddFrom.getAvatar().trim().equals("")) {
            userAddFrom.setAvatar("https://tse3-mm.cn.bing.net/th/id/OIP.LD7z0pQCYNX8CJ2Owko4LwAAAA?pid=Api&rs=1");
        }

        log.info("创建用户|{}", userAddFrom);
        return userService.create(new User(userAddFrom.getUsername(), userAddFrom.getPassword(), userAddFrom.getAvatar()));
    }

    @ApiOperation("删除用户")
    @GetMapping("delete")
    public R delete(Integer userId) {
        log.info("删除用户|{}", userId);
        return userService.delete(userId);
    }

    @ApiOperation("更新用户")
    @PostMapping("update")
    public R update(User user) {
        log.info("更新用户|{}", user);
        return userService.update(user);
    }

    @ApiOperation("用户列表")
    @GetMapping("list")
    public R list(int pageNo, int pageSize) {
        log.info("用户列表|{}|{}", pageNo, pageSize);
        return userService.findAll(pageNo, pageSize);
    }

}
